<?php

namespace app\admin\controller\sport;

use app\common\controller\Backend;
use app\admin\model\sport\Setting as SettingModel;

/**
 * 公用设置
 *
 * @icon fa fa-circle-o
 */
class Setting extends Backend
{

    /**
     * Setting模型对象
     * @var \app\admin\model\sport\Setting
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sport\Setting;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index($ids = null)
    {
        $allSettings = SettingModel::getSettings(); 
        $indexAdv = SettingModel::getSingleSetting('index_adv', $allSettings);
        $innerAdv = SettingModel::getSingleSetting('inner_adv', $allSettings);
        $skipArea = SettingModel::getSingleSetting('skip_area', $allSettings);
        $privateTeacher = SettingModel::getSingleSetting('private_teacher', $allSettings);
        $softAgreements = SettingModel::getSingleSetting('soft_agreements', $allSettings);
        $stepNote = SettingModel::getSingleSetting('step_note', $allSettings);

        $indexPic = SettingModel::getSingleSetting('index_pic', $allSettings);
        $innerPic = SettingModel::getSingleSetting('inner_pic', $allSettings);

        if ($this->request->isPost()) {
            SettingModel::where('name', 'index_adv')
              ->update(['value' => $this->request->post('index_adv')]);

            SettingModel::where('name', 'inner_adv')
              ->update(['value' => $this->request->post('inner_adv')]);

            SettingModel::where('name', 'skip_area')
              ->update(['value' => $this->request->post('skip_area')]);

            SettingModel::where('name', 'private_teacher')
              ->update(['value' => $this->request->post('private_teacher')]);

            SettingModel::where('name', 'soft_agreements')
              ->update(['value' => $this->request->post('soft_agreements')]);

            SettingModel::where('name', 'step_note')
              ->update(['value' => $this->request->post('step_note')]);

            SettingModel::where('name', 'index_pic')
              ->update(['value' => $this->request->post('index_pic')]);

            SettingModel::where('name', 'inner_pic')
              ->update(['value' => $this->request->post('inner_pic')]);

            $this->success("保存成功");

        }
        $this->view->assign('index_adv', $indexAdv);
        $this->view->assign('inner_adv', $innerAdv);
        $this->view->assign('skip_area', $skipArea);
        $this->view->assign('private_teacher', $privateTeacher);
        $this->view->assign('soft_agreements', $softAgreements);
        $this->view->assign('step_note', $stepNote);

        $this->view->assign('index_pic', $indexPic);
        $this->view->assign('inner_pic', $innerPic);

        return $this->view->fetch();
    }

}
